home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / string / tester.c < prev   
C/C++ Source or Header  |  1993-05-16  |  20KB  |  571 lines

  1. #include <ansidecl.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <strings.h>
  7. #include <fcntl.h>
  8.  
  9. #ifndef HAVE_GNU_LD
  10. #define _sys_nerr    sys_nerr
  11. #define _sys_errlist    sys_errlist
  12. #endif
  13.  
  14. #define    STREQ(a, b)    (strcmp((a), (b)) == 0)
  15.  
  16. CONST char *it = "<UNSET>";    /* Routine name for message routines. */
  17. size_t errors = 0;
  18.  
  19. /* Complain if condition is not true.  */
  20. void
  21. DEFUN(check, (thing, number), int thing AND int number)
  22. {
  23.   if (!thing)
  24.     {
  25.       printf("%s flunked test %d\n", it, number);
  26.       ++errors;
  27.     }
  28. }
  29.  
  30. /* Complain if first two args don't strcmp as equal.  */
  31. void
  32. DEFUN(equal, (a, b, number), CONST char *a AND CONST char *b AND int number)
  33. {
  34.   check(a != NULL && b != NULL && STREQ(a, b), number);
  35. }
  36.  
  37. char one[50];
  38. char two[50];
  39.  
  40. int
  41. DEFUN(main, (argc, argv), int argc AND char **argv)
  42. {
  43.   /* Test strcmp first because we use it to test other things.  */
  44.   it = "strcmp";
  45.   check(strcmp("", "") == 0, 1);        /* Trivial case. */
  46.   check(strcmp("a", "a") == 0, 2);        /* Identity. */
  47.   check(strcmp("abc", "abc") == 0, 3);        /* Multicharacter. */
  48.   check(strcmp("abc", "abcd") < 0, 4);        /* Length mismatches. */
  49.   check(strcmp("abcd", "abc") > 0, 5);
  50.   check(strcmp("abcd", "abce") < 0, 6);        /* Honest miscompares. */
  51.   check(strcmp("abce", "abcd") > 0, 7);
  52.   check(strcmp("a\203", "a") > 0, 8);        /* Tricky if char signed. */
  53.   check(strcmp("a\203", "a\003") > 0, 9);
  54.  
  55.   /* Test strcpy next because we need it to set up other tests.  */
  56.   it = "strcpy";
  57.   check(strcpy(one, "abcd") == one, 1);    /* Returned value. */
  58.   equal(one, "abcd", 2);        /* Basic test. */
  59.  
  60.   (void) strcpy(one, "x");
  61.   equal(one, "x", 3);            /* Writeover. */
  62.   equal(one+2, "cd", 4);        /* Wrote too much? */
  63.  
  64.   (void) strcpy(two, "hi there");
  65.   (void) strcpy(one, two);
  66.   equal(one, "hi there", 5);        /* Basic test encore. */
  67.   equal(two, "hi there", 6);        /* Stomped on source? */
  68.  
  69.   (void) strcpy(one, "");
  70.   equal(one, "", 7);            /* Boundary condition. */
  71.  
  72.   /* strcat.  */
  73.   it = "strcat";
  74.   (void) strcpy(one, "ijk");
  75.   check(strcat(one, "lmn") == one, 1);    /* Returned value. */
  76.   equal(one, "ijklmn", 2);        /* Basic test. */
  77.  
  78.   (void) strcpy(one, "x");
  79.   (void) strcat(one, "yz");
  80.   equal(one, "xyz", 3);            /* Writeover. */
  81.   equal(one+4, "mn", 4);            /* Wrote too much? */
  82.  
  83.   (void) strcpy(one, "gh");
  84.   (void) strcpy(two, "ef");
  85.   (void) strcat(one, two);
  86.   equal(one, "ghef", 5);            /* Basic test encore. */
  87.   equal(two, "ef", 6);            /* Stomped on source? */
  88.  
  89.   (void) strcpy(one, "");
  90.   (void) strcat(one, "");
  91.   equal(one, "", 7);            /* Boundary conditions. */
  92.   (void) strcpy(one, "ab");
  93.   (void) strcat(one, "");
  94.   equal(one, "ab", 8);
  95.   (void) strcpy(one, "");
  96.   (void) strcat(one, "cd");
  97.   equal(one, "cd", 9);
  98.  
  99.   /* strncat - first test it as strcat, with big counts,
  100.      then test the count mechanism.  */
  101.   it = "strncat";
  102.   (void) strcpy(one, "ijk");
  103.   check(strncat(one, "lmn", 99) == one, 1);    /* Returned value. */
  104.   equal(one, "ijklmn", 2);        /* Basic test. */
  105.  
  106.   (void) strcpy(one, "x");
  107.   (void) strncat(one, "yz", 99);
  108.   equal(one, "xyz", 3);            /* Writeover. */
  109.   equal(one+4, "mn", 4);            /* Wrote too much? */
  110.  
  111.   (void) strcpy(one, "gh");
  112.   (void) strcpy(two, "ef");
  113.   (void) strncat(one, two, 99);
  114.   equal(one, "ghef", 5);            /* Basic test encore. */
  115.   equal(two, "ef", 6);            /* Stomped on source? */
  116.  
  117.   (void) strcpy(one, "");
  118.   (void) strncat(one, "", 99);
  119.   equal(one, "", 7);            /* Boundary conditions. */
  120.   (void) strcpy(one, "ab");
  121.   (void) strncat(one, "", 99);
  122.   equal(one, "ab", 8);
  123.   (void) strcpy(one, "");
  124.   (void) strncat(one, "cd", 99);
  125.   equal(one, "cd", 9);
  126.  
  127.   (void) strcpy(one, "ab");
  128.   (void) strncat(one, "cdef", 2);
  129.   equal(one, "abcd", 10);            /* Count-limited. */
  130.  
  131.   (void) strncat(one, "gh", 0);
  132.   equal(one, "abcd", 11);            /* Zero count. */
  133.  
  134.   (void) strncat(one, "gh", 2);
  135.   equal(one, "abcdgh", 12);        /* Count and length equal. */
  136.  
  137.   /* strncmp - first test as strcmp with big counts,
  138.      then test count code.  */
  139.   it = "strncmp";
  140.   check(strncmp("", "", 99) == 0, 1);    /* Trivial case. */
  141.   check(strncmp("a", "a", 99) == 0, 2);    /* Identity. */
  142.   check(strncmp("abc", "abc", 99) == 0, 3);    /* Multicharacter. */
  143.   check(strncmp("abc", "abcd", 99) < 0, 4);    /* Length unequal. */
  144.   check(strncmp("abcd", "abc", 99) > 0, 5);
  145.   check(strncmp("abcd", "abce", 99) < 0, 6);    /* Honestly unequal. */
  146.   check(strncmp("abce", "abcd", 99) > 0, 7);
  147.   check(strncmp("a\203", "a", 2) > 0, 8);    /* Tricky if '\203' < 0 */
  148.   check(strncmp("a\203", "a\003", 2) > 0, 9);
  149.   check(strncmp("abce", "abcd", 3) == 0, 10);    /* Count limited. */
  150.   check(strncmp("abce", "abc", 3) == 0, 11);    /* Count == length. */
  151.   check(strncmp("abcd", "abce", 4) < 0, 12);    /* Nudging limit. */
  152.   check(strncmp("abc", "def", 0) == 0, 13);    /* Zero count. */
  153.  
  154.   /* strncpy - testing is a bit different because of odd semantics.  */
  155.   it = "strncpy";
  156.   check(strncpy(one, "abc", 4) == one, 1);    /* Returned value. */
  157.   equal(one, "abc", 2);            /* Did the copy go right? */
  158.  
  159.   (void) strcpy(one, "abcdefgh");
  160.   (void) strncpy(one, "xyz", 2);
  161.   equal(one, "xycdefgh", 3);        /* Copy cut by count. */
  162.  
  163.   (void) strcpy(one, "abcdefgh");
  164.   (void) strncpy(one, "xyz", 3);        /* Copy cut just before NUL. */
  165.   equal(one, "xyzdefgh", 4);
  166.  
  167.   (void) strcpy(one, "abcdefgh");
  168.   (void) strncpy(one, "xyz", 4);        /* Copy just includes NUL. */
  169.   equal(one, "xyz", 5);
  170.   equal(one+4, "efgh", 6);        /* Wrote too much? */
  171.  
  172.   (void) strcpy(one, "abcdefgh");
  173.   (void) strncpy(one, "xyz", 5);        /* Copy includes padding. */
  174.   equal(one, "xyz", 7);
  175.   equal(one+4, "", 8);
  176.   equal(one+5, "fgh", 9);
  177.  
  178.   (void) strcpy(one, "abc");
  179.   (void) strncpy(one, "xyz", 0);        /* Zero-length copy. */
  180.   equal(one, "abc", 10);    
  181.  
  182.   (void) strncpy(one, "", 2);        /* Zero-length source. */
  183.   equal(one, "", 11);
  184.   equal(one+1, "", 12);    
  185.   equal(one+2, "c", 13);
  186.  
  187.   (void) strcpy(one, "hi there");
  188.   (void) strncpy(two, one, 9);
  189.   equal(two, "hi there", 14);        /* Just paranoia. */
  190.   equal(one, "hi there", 15);        /* Stomped on source? */
  191.  
  192.   /* strlen.  */
  193.   it = "strlen";
  194.   check(strlen("") == 0, 1);        /* Empty. */
  195.   check(strlen("a") == 1, 2);        /* Single char. */
  196.   check(strlen("abcd") == 4, 3);        /* Multiple chars. */
  197.  
  198.   /* strchr.  */
  199.   it = "strchr";
  200.   check(strchr("abcd", 'z') == NULL, 1);    /* Not found. */
  201.   (void) strcpy(one, "abcd");
  202.   check(strchr(one, 'c') == one+2, 2);    /* Basic test. */
  203.   check(strchr(one, 'd') == one+3, 3);    /* End of string. */
  204.   check(strchr(one, 'a') == one, 4);    /* Beginning. */
  205.   check(strchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  206.   (void) strcpy(one, "ababa");
  207.   check(strchr(one, 'b') == one+1, 6);    /* Finding first. */
  208.   (void) strcpy(one, "");
  209.   check(strchr(one, 'b') == NULL, 7);    /* Empty string. */
  210.   check(strchr(one, '\0') == one, 8);    /* NUL in empty string. */
  211.  
  212. #if 0
  213.   /* index - just like strchr.  */
  214.   it = "index";
  215.   check(index("abcd", 'z') == NULL, 1);    /* Not found. */
  216.   (void) strcpy(one, "abcd");
  217.   check(index(one, 'c') == one+2, 2);    /* Basic test. */
  218.   check(index(one, 'd') == one+3, 3);    /* End of string. */
  219.   check(index(one, 'a') == one, 4);    /* Beginning. */
  220.   check(index(one, '\0') == one+4, 5);    /* Finding NUL. */
  221.   (void) strcpy(one, "ababa");
  222.   check(index(one, 'b') == one+1, 6);    /* Finding first. */
  223.   (void) strcpy(one, "");
  224.   check(index(one, 'b') == NULL, 7);    /* Empty string. */
  225.   check(index(one, '\0') == one, 8);    /* NUL in empty string. */
  226. #endif
  227.  
  228.   /* strrchr.  */
  229.   it = "strrchr";
  230.   check(strrchr("abcd", 'z') == NULL, 1);    /* Not found. */
  231.   (void) strcpy(one, "abcd");
  232.   check(strrchr(one, 'c') == one+2, 2);    /* Basic test. */
  233.   check(strrchr(one, 'd') == one+3, 3);    /* End of string. */
  234.   check(strrchr(one, 'a') == one, 4);    /* Beginning. */
  235.   check(strrchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  236.   (void) strcpy(one, "ababa");
  237.   check(strrchr(one, 'b') == one+3, 6);    /* Finding last. */
  238.   (void) strcpy(one, "");
  239.   check(strrchr(one, 'b') == NULL, 7);    /* Empty string. */
  240.   check(strrchr(one, '\0') == one, 8);    /* NUL in empty string. */
  241.  
  242. #if 0
  243.   /* rindex - just like strrchr.  */
  244.   it = "rindex";
  245.   check(rindex("abcd", 'z') == NULL, 1);    /* Not found. */
  246.   (void) strcpy(one, "abcd");
  247.   check(rindex(one, 'c') == one+2, 2);    /* Basic test. */
  248.   check(rindex(one, 'd') == one+3, 3);    /* End of string. */
  249.   check(rindex(one, 'a') == one, 4);    /* Beginning. */
  250.   check(rindex(one, '\0') == one+4, 5);    /* Finding NUL. */
  251.   (void) strcpy(one, "ababa");
  252.   check(rindex(one, 'b') == one+3, 6);    /* Finding last. */
  253.   (void) strcpy(one, "");
  254.   check(rindex(one, 'b') == NULL, 7);    /* Empty string. */
  255.   check(rindex(one, '\0') == one, 8);    /* NUL in empty string. */
  256. #endif
  257.  
  258.   /* strpbrk - somewhat like strchr.  */
  259.   it = "strpbrk";
  260.   check(strpbrk("abcd", "z") == NULL, 1);    /* Not found. */
  261.   (void) strcpy(one, "abcd");
  262.   check(strpbrk(one, "c") == one+2, 2);    /* Basic test. */
  263.   check(strpbrk(one, "d") == one+3, 3);    /* End of string. */
  264.   check(strpbrk(one, "a") == one, 4);    /* Beginning. */
  265.   check(strpbrk(one, "") == NULL, 5);    /* Empty search list. */
  266.   check(strpbrk(one, "cb") == one+1, 6);    /* Multiple search. */
  267.   (void) strcpy(one, "abcabdea");
  268.   check(strpbrk(one, "b") == one+1, 7);    /* Finding first. */
  269.   check(strpbrk(one, "cb") == one+1, 8);    /* With multiple search. */
  270.   check(strpbrk(one, "db") == one+1, 9);    /* Another variant. */
  271.   (void) strcpy(one, "");
  272.   check(strpbrk(one, "bc") == NULL, 10);    /* Empty string. */
  273.   check(strpbrk(one, "") == NULL, 11);    /* Both strings empty. */
  274.  
  275.   /* strstr - somewhat like strchr.  */
  276.   it = "strstr";
  277.   check(strstr("abcd", "z") == NULL, 1);    /* Not found. */
  278.   check(strstr("abcd", "abx") == NULL, 2);    /* Dead end. */
  279.   (void) strcpy(one, "abcd");
  280.   check(strstr(one, "c") == one+2, 3);    /* Basic test. */
  281.   check(strstr(one, "bc") == one+1, 4);    /* Multichar. */
  282.   check(strstr(one, "d") == one+3, 5);    /* End of string. */
  283.   check(strstr(one, "cd") == one+2, 6);    /* Tail of string. */
  284.   check(strstr(one, "abc") == one, 7);    /* Beginning. */
  285.   check(strstr(one, "abcd") == one, 8);    /* Exact match. */
  286.   check(strstr(one, "abcde") == NULL, 9);    /* Too long. */
  287.   check(strstr(one, "de") == NULL, 10);    /* Past end. */
  288.   check(strstr(one, "") == one, 11);    /* Finding empty. */
  289.   (void) strcpy(one, "ababa");
  290.   check(strstr(one, "ba") == one+1, 12);    /* Finding first. */
  291.   (void) strcpy(one, "");
  292.   check(strstr(one, "b") == NULL, 13);    /* Empty string. */
  293.   check(strstr(one, "") == one, 14);    /* Empty in empty string. */
  294.   (void) strcpy(one, "bcbca");
  295.   check(strstr(one, "bca") == one+2, 15);    /* False start. */
  296.   (void) strcpy(one, "bbbcabbca");
  297.   check(strstr(one, "bbca") == one+1, 16);    /* With overlap. */
  298.  
  299.   /* strspn.  */
  300.   it = "strspn";
  301.   check(strspn("abcba", "abc") == 5, 1);    /* Whole string. */
  302.   check(strspn("abcba", "ab") == 2, 2);    /* Partial. */
  303.   check(strspn("abc", "qx") == 0, 3);    /* None. */
  304.   check(strspn("", "ab") == 0, 4);    /* Null string. */
  305.   check(strspn("abc", "") == 0, 5);    /* Null search list. */
  306.  
  307.   /* strcspn.  */
  308.   it = "strcspn";
  309.   check(strcspn("abcba", "qx") == 5, 1);    /* Whole string. */
  310.   check(strcspn("abcba", "cx") == 2, 2);    /* Partial. */
  311.   check(strcspn("abc", "abc") == 0, 3);    /* None. */
  312.   check(strcspn("", "ab") == 0, 4);    /* Null string. */
  313.   check(strcspn("abc", "") == 3, 5);    /* Null search list. */
  314.  
  315.   /* strtok - the hard one.  */
  316.   it = "strtok";
  317.   (void) strcpy(one, "first, second, third");
  318.   equal(strtok(one, ", "), "first", 1);    /* Basic test. */
  319.   equal(one, "first", 2);
  320.   equal(strtok((char *)NULL, ", "), "second", 3);
  321.   equal(strtok((char *)NULL, ", "), "third", 4);
  322.   check(strtok((char *)NULL, ", ") == NULL, 5);
  323.   (void) strcpy(one, ", first, ");
  324.   equal(strtok(one, ", "), "first", 6);    /* Extra delims, 1 tok. */
  325.   check(strtok((char *)NULL, ", ") == NULL, 7);
  326.   (void) strcpy(one, "1a, 1b; 2a, 2b");
  327.   equal(strtok(one, ", "), "1a", 8);    /* Changing delim lists. */
  328.   equal(strtok((char *)NULL, "; "), "1b", 9);
  329.   equal(strtok((char *)NULL, ", "), "2a", 10);
  330.   (void) strcpy(two, "x-y");
  331.   equal(strtok(two, "-"), "x", 11);    /* New string before done. */
  332.   equal(strtok((char *)NULL, "-"), "y", 12);
  333.   check(strtok((char *)NULL, "-") == NULL, 13);
  334.   (void) strcpy(one, "a,b, c,, ,d");
  335.   equal(strtok(one, ", "), "a", 14);    /* Different separators. */
  336.   equal(strtok((char *)NULL, ", "), "b", 15);
  337.   equal(strtok((char *)NULL, " ,"), "c", 16);    /* Permute list too. */
  338.   equal(strtok((char *)NULL, " ,"), "d", 17);
  339.   check(strtok((char *)NULL, ", ") == NULL, 18);
  340.   check(strtok((char *)NULL, ", ") == NULL, 19);    /* Persistence. */
  341.   (void) strcpy(one, ", ");
  342.   check(strtok(one, ", ") == NULL, 20);    /* No tokens. */
  343.   (void) strcpy(one, "");
  344.   check(strtok(one, ", ") == NULL, 21);    /* Empty string. */
  345.   (void) strcpy(one, "abc");
  346.   equal(strtok(one, ", "), "abc", 22);    /* No delimiters. */
  347.   check(strtok((char *)NULL, ", ") == NULL, 23);
  348.   (void) strcpy(one, "abc");
  349.   equal(strtok(one, ""), "abc", 24);    /* Empty delimiter list. */
  350.   check(strtok((char *)NULL, "") == NULL, 25);
  351.   (void) strcpy(one, "abcdefgh");
  352.   (void) strcpy(one, "a,b,c");
  353.   equal(strtok(one, ","), "a", 26);    /* Basics again... */
  354.   equal(strtok((char *)NULL, ","), "b", 27);
  355.   equal(strtok((char *)NULL, ","), "c", 28);
  356.   check(strtok((char *)NULL, ",") == NULL, 29);
  357.   equal(one+6, "gh", 30);            /* Stomped past end? */
  358.   equal(one, "a", 31);            /* Stomped old tokens? */
  359.   equal(one+2, "b", 32);
  360.   equal(one+4, "c", 33);
  361.  
  362.   /* memcmp.  */
  363.   it = "memcmp";
  364.   check(memcmp("a", "a", 1) == 0, 1);    /* Identity. */
  365.   check(memcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  366.   check(memcmp("abcd", "abce", 4) < 0, 3);    /* Honestly unequal. */
  367.   check(memcmp("abce", "abcd", 4) > 0, 4);
  368.   check(memcmp("alph", "beta", 4) < 0, 5);
  369.   check(memcmp("a\203", "a\003", 2) > 0, 6);
  370.   check(memcmp("abce", "abcd", 3) == 0, 7);    /* Count limited. */
  371.   check(memcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  372.  
  373.   /* memchr.  */
  374.   it = "memchr";
  375.   check(memchr("abcd", 'z', 4) == NULL, 1);    /* Not found. */
  376.   (void) strcpy(one, "abcd");
  377.   check(memchr(one, 'c', 4) == one+2, 2);    /* Basic test. */
  378.   check(memchr(one, 'd', 4) == one+3, 3);    /* End of string. */
  379.   check(memchr(one, 'a', 4) == one, 4);    /* Beginning. */
  380.   check(memchr(one, '\0', 5) == one+4, 5);    /* Finding NUL. */
  381.   (void) strcpy(one, "ababa");
  382.   check(memchr(one, 'b', 5) == one+1, 6);    /* Finding first. */
  383.   check(memchr(one, 'b', 0) == NULL, 7);    /* Zero count. */
  384.   check(memchr(one, 'a', 1) == one, 8);    /* Singleton case. */
  385.   (void) strcpy(one, "a\203b");
  386.   check(memchr(one, 0203, 3) == one+1, 9);    /* Unsignedness. */
  387.  
  388.   /* memcpy - need not work for overlap.  */
  389.   it = "memcpy";
  390.   check(memcpy(one, "abc", 4) == one, 1);    /* Returned value. */
  391.   equal(one, "abc", 2);            /* Did the copy go right? */
  392.  
  393.   (void) strcpy(one, "abcdefgh");
  394.   (void) memcpy(one+1, "xyz", 2);
  395.   equal(one, "axydefgh", 3);        /* Basic test. */
  396.  
  397.   (void) strcpy(one, "abc");
  398.   (void) memcpy(one, "xyz", 0);
  399.   equal(one, "abc", 4);            /* Zero-length copy. */
  400.  
  401.   (void) strcpy(one, "hi there");
  402.   (void) strcpy(two, "foo");
  403.   (void) memcpy(two, one, 9);
  404.   equal(two, "hi there", 5);        /* Just paranoia. */
  405.   equal(one, "hi there", 6);        /* Stomped on source? */
  406.  
  407.   /* memmove - must work on overlap.  */
  408.   it = "memmove";
  409.   check(memmove(one, "abc", 4) == one, 1);    /* Returned value. */
  410.   equal(one, "abc", 2);            /* Did the copy go right? */
  411.  
  412.   (void) strcpy(one, "abcdefgh");
  413.   (void) memmove(one+1, "xyz", 2);
  414.   equal(one, "axydefgh", 3);        /* Basic test. */
  415.  
  416.   (void) strcpy(one, "abc");
  417.   (void) memmove(one, "xyz", 0);
  418.   equal(one, "abc", 4);            /* Zero-length copy. */
  419.  
  420.   (void) strcpy(one, "hi there");
  421.   (void) strcpy(two, "foo");
  422.   (void) memmove(two, one, 9);
  423.   equal(two, "hi there", 5);        /* Just paranoia. */
  424.   equal(one, "hi there", 6);        /* Stomped on source? */
  425.  
  426.   (void) strcpy(one, "abcdefgh");
  427.   (void) memmove(one+1, one, 9);
  428.   equal(one, "aabcdefgh", 7);        /* Overlap, right-to-left. */
  429.  
  430.   (void) strcpy(one, "abcdefgh");
  431.   (void) memmove(one+1, one+2, 7);
  432.   equal(one, "acdefgh", 8);        /* Overlap, left-to-right. */
  433.  
  434.   (void) strcpy(one, "abcdefgh");
  435.   (void) memmove(one, one, 9);
  436.   equal(one, "abcdefgh", 9);        /* 100% overlap. */
  437.  
  438.   /* memccpy - first test like memcpy, then the search part
  439.      The SVID, the only place where memccpy is mentioned, says
  440.      overlap might fail, so we don't try it.  Besides, it's hard
  441.      to see the rationale for a non-left-to-right memccpy.  */
  442.   it = "memccpy";
  443.   check(memccpy(one, "abc", 'q', 4) == NULL, 1);    /* Returned value. */
  444.   equal(one, "abc", 2);            /* Did the copy go right? */
  445.  
  446.   (void) strcpy(one, "abcdefgh");
  447.   (void) memccpy(one+1, "xyz", 'q', 2);
  448.   equal(one, "axydefgh", 3);        /* Basic test. */
  449.  
  450.   (void) strcpy(one, "abc");
  451.   (void) memccpy(one, "xyz", 'q', 0);
  452.   equal(one, "abc", 4);            /* Zero-length copy. */
  453.  
  454.   (void) strcpy(one, "hi there");
  455.   (void) strcpy(two, "foo");
  456.   (void) memccpy(two, one, 'q', 9);
  457.   equal(two, "hi there", 5);        /* Just paranoia. */
  458.   equal(one, "hi there", 6);        /* Stomped on source? */
  459.  
  460.   (void) strcpy(one, "abcdefgh");
  461.   (void) strcpy(two, "horsefeathers");
  462.   check(memccpy(two, one, 'f', 9) == two+6, 7);    /* Returned value. */
  463.   equal(one, "abcdefgh", 8);        /* Source intact? */
  464.   equal(two, "abcdefeathers", 9);        /* Copy correct? */
  465.  
  466.   (void) strcpy(one, "abcd");
  467.   (void) strcpy(two, "bumblebee");
  468.   check(memccpy(two, one, 'a', 4) == two+1, 10);    /* First char. */
  469.   equal(two, "aumblebee", 11);
  470.   check(memccpy(two, one, 'd', 4) == two+4, 12);    /* Last char. */
  471.   equal(two, "abcdlebee", 13);
  472.   (void) strcpy(one, "xyz");
  473.   check(memccpy(two, one, 'x', 1) == two+1, 14);    /* Singleton. */
  474.   equal(two, "xbcdlebee", 15);
  475.  
  476.   /* memset.  */
  477.   it = "memset";
  478.   (void) strcpy(one, "abcdefgh");
  479.   check(memset(one+1, 'x', 3) == one+1, 1);    /* Return value. */
  480.   equal(one, "axxxefgh", 2);        /* Basic test. */
  481.  
  482.   (void) memset(one+2, 'y', 0);
  483.   equal(one, "axxxefgh", 3);        /* Zero-length set. */
  484.  
  485.   (void) memset(one+5, 0, 1);
  486.   equal(one, "axxxe", 4);            /* Zero fill. */
  487.   equal(one+6, "gh", 5);            /* And the leftover. */
  488.  
  489.   (void) memset(one+2, 010045, 1);
  490.   equal(one, "ax\045xe", 6);        /* Unsigned char convert. */
  491.  
  492.   /* bcopy - much like memcpy.
  493.      Berklix manual is silent about overlap, so don't test it.  */
  494.   it = "bcopy";
  495.   (void) bcopy("abc", one, 4);
  496.   equal(one, "abc", 1);            /* Simple copy. */
  497.  
  498.   (void) strcpy(one, "abcdefgh");
  499.   (void) bcopy("xyz", one+1, 2);
  500.   equal(one, "axydefgh", 2);        /* Basic test. */
  501.  
  502.   (void) strcpy(one, "abc");
  503.   (void) bcopy("xyz", one, 0);
  504.   equal(one, "abc", 3);            /* Zero-length copy. */
  505.  
  506.   (void) strcpy(one, "hi there");
  507.   (void) strcpy(two, "foo");
  508.   (void) bcopy(one, two, 9);
  509.   equal(two, "hi there", 4);        /* Just paranoia. */
  510.   equal(one, "hi there", 5);        /* Stomped on source? */
  511.  
  512.   /* bzero.  */
  513.   it = "bzero";
  514.   (void) strcpy(one, "abcdef");
  515.   bzero(one+2, 2);
  516.   equal(one, "ab", 1);            /* Basic test. */
  517.   equal(one+3, "", 2);
  518.   equal(one+4, "ef", 3);
  519.  
  520.   (void) strcpy(one, "abcdef");
  521.   bzero(one+2, 0);
  522.   equal(one, "abcdef", 4);        /* Zero-length copy. */
  523.  
  524. #if 0
  525.   /* bcmp - somewhat like memcmp.  */
  526.   it = "bcmp";
  527.   check(bcmp("a", "a", 1) == 0, 1);    /* Identity. */
  528.   check(bcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  529.   check(bcmp("abcd", "abce", 4) != 0, 3);    /* Honestly unequal. */
  530.   check(bcmp("abce", "abcd", 4) != 0, 4);
  531.   check(bcmp("alph", "beta", 4) != 0, 5);
  532.   check(bcmp("abce", "abcd", 3) == 0, 6);    /* Count limited. */
  533.   check(bcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  534. #endif
  535.  
  536.   {
  537.     char text[] = "This,is,a,test";
  538.     char *list = text;
  539.     it = "strsep";
  540.     check (!strcmp ("This", strsep (&list, ",")), 1);
  541.     check (!strcmp ("is", strsep (&list, ",")), 2);
  542.     check (!strcmp ("a", strsep (&list, ",")), 3);
  543.     check (!strcmp ("test", strsep (&list, ",")), 4);
  544.     check (strsep (&list, ",") == NULL, 5);
  545.   }
  546.  
  547.   /* strerror - VERY system-dependent.  */
  548.   {
  549.     int f;
  550.     it = "strerror";
  551.     f = __open("/", O_WRONLY);    /* Should always fail. */
  552.     check(f < 0 && errno > 0 && errno < _sys_nerr, 1);
  553.     equal(strerror(errno), _sys_errlist[errno], 2);
  554.   }
  555.  
  556.   {
  557.     int status;
  558.     if (errors == 0)
  559.       {
  560.     status = EXIT_SUCCESS;
  561.     puts("No errors.");
  562.       }
  563.     else
  564.       {
  565.     status = EXIT_FAILURE;
  566.     printf("%Z errors.\n", errors);
  567.       }
  568.     exit(status);
  569.   }
  570. }
  571.